﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dukous.Security.Domain
{
    /// <summary>
    /// 组
    /// </summary>
    [Serializable]
    public class Group : DomainEntity
    {
        /// <summary>
        /// 获取 / 设置 父组
        /// </summary>
        public virtual Group Parent { get; set; }

        /// <summary>
        /// 获取 / 设置 名称
        /// </summary>
        public virtual string GroupName { get; set; }

        /// <summary>
        /// 获取 / 设置 描述
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// 获取 / 设置 用户
        /// </summary>
        public virtual IList<User> Users { get; set; }

        /// <summary>
        /// 获取 / 设置 对象权限
        /// </summary>
        public virtual IList<SecurityObjectPermission> SecurityObjectPermissions { get; set; }

        /// <summary>
        /// 添加一个用户（会自动在 User 中建立相应关联）
        /// </summary>
        /// <param name="user"></param>
        public virtual void AddUser(User user)
        {
            InternalAddUser(user);
            user.InternalAddGroup(this);
        }

        /// <summary>
        /// 移除一个帐号（会自动在 User 中删除相应关联）
        /// </summary>
        /// <param name="account"></param>
        public virtual void RemoveUser(User user)
        {
            InternalRemoveUser(user);
            user.InternalRemoveGroup(this);
        }

        /// <summary>
        /// 添加一个用户
        /// </summary>
        /// <param name="user"></param>
        protected internal virtual void InternalAddUser(User user)
        {
            //Check.Require(user != null);

            if (this.Users == null)
            {
                this.Users = new List<User>();
            }
            this.Users.Add(user);
        }

        /// <summary>
        /// 移除一个用户
        /// </summary>
        /// <param name="user"></param>
        protected internal virtual void InternalRemoveUser(User user)
        {
            //Check.Require(user != null);

            if (this.Users == null)
            {
                this.Users = new List<User>();
            }
            this.Users.Remove(user);
        }
    }
}
